package club.emergency.wechat.department.operate.provider.impl;

import club.emergency.wechat.common.response.ResponseEntity;
import club.emergency.wechat.common.response.StatusCode;
import club.emergency.wechat.department.operate.DepartmentOperate;
import club.emergency.wechat.model.department.Department;
import club.emergency.wechat.persistence.department.DepartmentMapper;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigInteger;
import java.util.Objects;

/**
 * Create with IntelliJ IDEA
 * Project Name: wechat
 * Package Name: club.emergency.wechat.department.operate.provider.impl
 * Date: 2019/3/20
 *
 * @author: 快乐的小菌菌
 * Description:
 */
@RestController
public class DepartmentOperateImpl implements DepartmentOperate {

    private DepartmentMapper departmentMapper;

    public DepartmentOperateImpl(DepartmentMapper departmentMapper) {
        this.departmentMapper = departmentMapper;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String save(@RequestBody Department department, @RequestParam(value = "parentCode", required = false) String parentCode) {
        if (Objects.nonNull(parentCode) && parentCode.length() >= 28) {
            return ResponseEntity.ok(StatusCode.DB_SAVE_ERROR, "新增企业信息失败！已超过最大级别限制！");
        }
        Integer checkOnly = departmentMapper.checkOnly(department.getDepartmentName());
        if (!checkOnly.equals(0)) {
            return ResponseEntity.ok(StatusCode.DB_SAVE_ERROR, "新增企业信息失败！已有重复名称的企业！");
        }
        if (Objects.isNull(department.getParentId()) && Objects.isNull(parentCode)) {
            String count = departmentMapper.queryParentIdIsNull();
            if (Objects.isNull(count)) {
                department.setCode("1000");
            } else {
                BigInteger bigInteger = new BigInteger(String.valueOf(count));
                department.setCode(String.valueOf(bigInteger.add(BigInteger.ONE)));
            }
            departmentMapper.insert(department);
        } else {
            String maxCodeWithParentId = departmentMapper.queryMaxCodeWithParentId(parentCode + "%", department.getParentId());
            if (Objects.isNull(maxCodeWithParentId)) {
                department.setCode(parentCode + "1000");
            } else {
                BigInteger bigInteger = new BigInteger(maxCodeWithParentId);
                department.setCode(String.valueOf(bigInteger.add(BigInteger.ONE)));
            }
            departmentMapper.insert(department);
        }
        return ResponseEntity.ok(StatusCode.DB_SAVE_OK, "新增企业信息成功！", department.getId());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String modify(@RequestBody Department department, @RequestParam(value = "parentCode", required = false) String parentCode) {
        if (Objects.nonNull(parentCode) && parentCode.length() >= 28) {
            return ResponseEntity.ok(StatusCode.DB_SAVE_ERROR, "新增企业信息失败！已超过最大级别限制！");
        }
        Integer checkOnlyForEdit = departmentMapper.checkOnlyForEdit(department.getDepartmentName(), department.getId());
        if (!checkOnlyForEdit.equals(0)) {
            return ResponseEntity.ok(StatusCode.DB_MODIFY_ERROR, "修改企业信息失败！已有重复名称的企业！");
        }
        departmentMapper.updateById(department);
        return ResponseEntity.ok(StatusCode.DB_MODIFY_OK, "修改企业信息成功！");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String remove(@RequestParam("id") Long id) {
        Integer count = departmentMapper.deleteCheckChildren(id);
        if (!count.equals(0)) {
            return ResponseEntity.ok(StatusCode.DB_REMOVE_ERROR, "删除企业信息失败！请先删除子企业！");
        }
        departmentMapper.deleteById(id);
        return ResponseEntity.ok(StatusCode.DB_REMOVE_OK, "删除企业信息成功！");
    }

}
